import 'package:flutter/material.dart';
import 'package:hzy_project_template/base_ui_config/base_ui_config_index.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

import '../../../commpontents/commpontents_index.dart';
import '../../../config/config_index.dart';
import '../models/tu_chong_model.dart';

class ExampleTuItmeImagelessV extends CommonLessWidget {
  const ExampleTuItmeImagelessV({
    super.key,
    required this.tuChongItem,
    this.heroTag = "tuChongItem",
    this.type = 0,
  });
  final TuChongItem tuChongItem;

  final String heroTag;
  final int type;
  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = configImageListWidget();

    body = ClipRRect(
      borderRadius: BorderRadius.circular(10.r),
      child: body,
    );
    body = Container(
      padding: const EdgeInsets.all(5),
      decoration: BoxDecoration(
        color: HzyCommonColor().backgroudgrey,
        borderRadius: BorderRadius.circular(
          10.r,
        ),
      ),
      child: body,
    );
    return body;
  }

  /// 配置图片数组视图
  configImageListWidget() {
    if (tuChongItem.realImages?.length == 1) {
      return configOnlyOneImageWidget();
    } else {
      return GridView.count(
        crossAxisCount: tuChongItem.crossAxisCount ?? 1,
        childAspectRatio: tuChongItem.crossAxisCount == 2 ? 1 : 1,
        crossAxisSpacing: 5.0,
        mainAxisSpacing: 5.0,
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        children: [
          ...List.generate(
              tuChongItem.realImages!.length > 9
                  ? 9
                  : tuChongItem.realImages!.length, (index) {
            Widget body = configImageItem(
              imageUrl: tuChongItem.realImages![index],
              index: index,
              allNum: tuChongItem.images?.length ?? 0,
              heroTag: heroTag,
            );
            return body;
          })
        ],
      );
    }
  }

  /// 配置单图视图
  configOnlyOneImageWidget() {
    Widget body = LayoutBuilder(
      builder: (p0, p1) {
        ImageItem imageItem = tuChongItem.images![0];
        double ratio = (imageItem.width ?? 0) / (imageItem.height ?? 1);
        double imageH = p1.maxWidth / ratio;
        Widget body = configTuchongImageWidget(
          url: imageItem.imageUrl,
          width: p1.maxWidth,
          height: imageH,
          index: 0,
          heroTag: heroTag,
        );
        return body;
      },
    );
    return body;
  }

  /// 配置图片item
  configImageItem({
    required String imageUrl,
    required int index,
    required int allNum,
    String? heroTag,
  }) {
    Widget body = LayoutBuilder(
      builder: (p0, p1) {
        /// 加载图片
        Widget body = configTuchongImageWidget(
          url: imageUrl,
          heroTag: heroTag,
          width: p1.maxWidth,
          height: p1.maxHeight,
          index: index,
          isTap: false,
        );
        return body;
      },
    );
    if (index == 8 && allNum > (index + 1)) {
      body = Stack(
        children: [
          body,
          Positioned.fill(
            child: configResidueNumWidget(
              num: allNum - 9,
            ),
          )
        ],
      );
    }
    body = InkWell(
      onTap: () {
        jumpToDetail(index);
      },
      child: body,
    );

    return body;
  }

  /// 配置图片显示视图
  configTuchongImageWidget({
    String? url,
    double? width,
    double? height,
    String? heroTag,
    bool isTap = true,
    required int index,
  }) {
    Widget body = configHeroNetImageWidget(
      url: url,
      width: width,
      height: height,
      heroTag: heroTag,
    );

    body = isTap
        ? InkWell(
            onTap: () {
              jumpToDetail(index);
            },
            child: body,
          )
        : body;

    return body;
  }

  /// 创建图片剩余个数
  configResidueNumWidget({required int num}) {
    Widget body = Text(
      "+$num",
      style: HzyTextStyle.fontBold(
        size: 16.sp,
        color: Colors.white,
      ),
    );
    body = Container(
      decoration: const BoxDecoration(
        color: Colors.black12,
      ),
      alignment: Alignment.center,
      child: body,
    );
    return body;
  }

  jumpToDetail(int index) {
    currentToPage(
      name: PageIdConfig.tuChongDetail,
      arguments: tuChongItem,
      parameters: {
        "index": "$index",
        "heroTag": type == 0 ? 'homeList' : "collectList",
      },
    );
  }
}
